package org.zjx.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.awt.*;

@Data
public class Rect {
    private int left;
    private int top;
    private int right;
    private int bottom;

    // 添加无参构造函数
    public Rect() {}

    public Rect(int left,int top,int right,int bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    public Rect(Rectangle rectangle) {
        this.left = rectangle.x;
        this.top = rectangle.y;
        this.right = rectangle.x + rectangle.width - 1;
        this.bottom = rectangle.y + rectangle.height - 1;
    }

    @JsonIgnore
    public int getWidth() {
        return right - left + 1;
    }

    @JsonIgnore
    public int getHeight() {
        return bottom - top + 1;
    }

    /**
     * 判断当前矩形是否与另一个矩形重叠
     * @param other 另一个矩形
     * @return 如果两个矩形有重叠区域则返回true，否则返回false
     */
    @JsonIgnore
    public boolean overlap(Rect other) {
        return !(this.right < other.left ||
                other.right < this.left ||
                this.bottom < other.top ||
                other.bottom < this.top);
    }

    @JsonIgnore
    public boolean contains(Point p) {
        return p.x >= left && p.x <= right && p.y >= top && p.y <= bottom;
    }

    @JsonIgnore
    public Point getCenter() {
        return new Point((left + right) / 2, (top + bottom) / 2);
    }

    public Rectangle toRectangle() {
        return new Rectangle(left, top, right - left + 1, bottom - top + 1);
    }

    @Override
    public String toString() {
        return String.format("[%d, %d, %d, %d]", left, top, right, bottom);
    }
}